﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text;
using NHibernate.Cfg;
using NHibernate;
using log4net;
using NHibernate.Criterion;
using BidFunLib.Entity;
using BidFunLib.DAO;


namespace BidFunLib
{
    /// <summary>
    /// 
    /// </summary>
    class StatsBidSaleDAO 
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(StatsBidUserByBidObjectDAO));

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public StatsBidUserByBidObject getStatsBidUserByObjectById(Int32 id)
        {
            StatsBidUserByBidObject statsBidUserByBidObject = null;

            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                IList<StatsBidUserByBidObject> statsBidUserByBidObjectList = (IList<StatsBidUserByBidObject>)session.CreateQuery("from StatsBidUserByObject bs where bs.IdStatsBidUserByObject= ?")
                .SetInt32(0, id)
                .List<StatsBidUserByBidObject>();

                if (statsBidUserByBidObjectList != null && statsBidUserByBidObjectList.Count > 0)
                {
                    statsBidUserByBidObject = statsBidUserByBidObjectList.First();
                }
                transaction.Commit();
                session.Close();
                return statsBidUserByBidObject;
            }
        }


        /// <summary>
        /// Retourne la liste des users qui ont enchéris sur une vente
        /// </summary>
        /// <param name="idBidSale"></param>
        /// <returns></returns>
        public IList<StatsBidSale> getAllStatsBidSaleForOneObject(Int32 idBidObject)
        {
            IList<StatsBidSale> statsBidSaleList = null;

            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                statsBidSaleList =
                    (IList<StatsBidSale>)session.CreateQuery("from StatsBidSale sbs where sbs.IdBidObject = ?")
                    .SetInt32(0, idBidObject)
                    .List<StatsBidSale>();

                transaction.Commit();
                session.Close();
                return statsBidSaleList;
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="pseudo"></param>
        /// <returns></returns>
        public StatsBidSale saveOrUpdate(StatsBidSale statsBidSale)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                if (statsBidSale.IdStatsBidSale == -1)
                {
                    log.Debug("Saving statsBidSale...");
                    Int32 newID = (Int32)session.Save(statsBidSale);
                    statsBidSale.IdStatsBidSale = newID;
                    log.Debug("statsBidSale saved !");
                }
                else
                {
                    log.Debug("Updating statsBidSale...");
                    session.Update(statsBidSale);
                    log.Debug("statsBidSale updated !");
                }
                transaction.Commit();
                session.Close();
                return statsBidSale;
            }
        }


    }
}
